package datastructure.common;

/**
 * @author cyt
 * @create 2020-11-22 上午 11:50
 */
public abstract class AbstractList<E> implements List<E> {
    protected int size;

    @Override
    public boolean isEmpty() {
        return size == 0;
    }

    @Override
    public void add(E e) {
        add(size, e);
    }

    @Override
    public int size() {
        return size;
    }

    @Override
    public boolean contain(E element) {
        return indexOf(element) == ELEMENT_NOT_FOUND;
    }

    protected void rangeCheck(int index) {
        if (index < 0 || index >= size) {
            OutOfBounds(index);
        }
    }

    protected void rangeCheckOfAdd(int index) {
        if (index < 0 || index > size) {
            OutOfBounds(index);
        }
    }

    private void OutOfBounds(int index) {
        throw new IndexOutOfBoundsException("Index: " + index + "   Size : " + size);
    }
}
